package com.guigu.gcservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.commonutils.R;
import com.guigu.gcservice.entity.Gongcheng;
import com.guigu.gcservice.entity.Hunning;
import com.guigu.gcservice.service.GongchengService;
import com.guigu.gcservice.service.HunningService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-05
 */
@RestController
@RequestMapping("/gcservice/hunning")
@CrossOrigin
public class HunningController {

    @Autowired
    private HunningService hunningService;

    //保存计算好的值
    @PostMapping("save")
    public R save(@RequestBody Hunning hunning) {
        double region = hunning.getRegion().doubleValue();
        double size = hunning.getSize().doubleValue();
        double shuini = hunning.getShuini().doubleValue();
        double dashi = hunning.getDashi().doubleValue();
        double zhongshi = hunning.getZhongshi().doubleValue();
        double xiaoshi = hunning.getXiaoshi().doubleValue();
        double sha = hunning.getSha().doubleValue();
        double waijia = hunning.getWaijia().doubleValue();

//        Double shuini1 = hunning.getShuini1();
//        Double dashi1 = hunning.getDashi1();
//        Double zhongshi1 = hunning.getZhongshi1();
//        Double xiaoshi1 = hunning.getXiaoshi1();
//        Double sha1 = hunning.getSha1();
//        Double waijia1 = hunning.getWaijia1();
//
//
//        Double shuini2 = hunning.getShuini2();
//        Double dashi2 = hunning.getDashi2();
//        Double zhongshi2 = hunning.getZhongshi2();
//        Double xiaoshi2 = hunning.getXiaoshi2();
//        Double sha2 = hunning.getSha2();
//        Double waijia2 = hunning.getWaijia2();


        if (region==20) {
            double shuini1 = size * (1 + shuini * 0.01) * 340/1000;
            double dashi1 = size * (1 + dashi * 0.01) * 1019/1000;
            double zhongshi1 = size * (1 + zhongshi * 0.01) * 1020/1000;
            double xiaoshi1 = size * (1 + xiaoshi * 0.01) * 1030/1000;
            double sha1 = size * (1 + sha * 0.01) * 1006/1000;
            double waijia1 = size * (1 + waijia * 0.01) * 5.32/1000;
            double total = shuini1 + dashi1 + zhongshi1 + xiaoshi1 + sha1 + waijia1;
            hunning.setTotal(total);
            hunning.setShuini1(shuini1);
            hunning.setDashi1(dashi1);
            hunning.setZhongshi1(zhongshi1);
            hunning.setXiaoshi1(xiaoshi1);
            hunning.setSha1(sha1);
            hunning.setWaijia1(waijia1);

            double shuini2 = shuini1 / total * 100;
            double dashi2 = dashi1 / total * 100;
            double zhongshi2 = zhongshi1 / total * 100;
            double xiaoshi2 = xiaoshi1 / total * 100;
            double sha2 = sha1 / total * 100;
            double waijia2 = waijia1 / total * 100;

            hunning.setShuini2(shuini2);
            hunning.setDashi2(dashi2);
            hunning.setZhongshi2(zhongshi2);
            hunning.setXiaoshi2(xiaoshi2);
            hunning.setSha2(sha2);
            hunning.setWaijia2(waijia2);


            System.out.println(waijia2);
            System.out.println(waijia1);
            System.out.println(shuini2); //2代表占比
            System.out.println(shuini1); //1代表材料数量


            boolean save = hunningService.save(hunning);
            if (save) {
                return R.ok().data("hunning",hunning);
            } else {
                return R.error();
            }


        }

        if (region==25) {
            double shuini1 = size * (1 + shuini * 0.01) * 270/1000;
            double dashi1 = size * (1 + dashi * 0.01) * 1001/1000;
            double zhongshi1 = size * (1 + zhongshi * 0.01) * 1021/1000;
            double xiaoshi1 = size * (1 + xiaoshi * 0.01) * 1500/1000;
            double sha1 = size * (1 + sha * 0.01) * 880/1000;
            double waijia1 = size * (1 + waijia * 0.01) * 5.32/1000;
            double total = shuini1 + dashi1 + zhongshi1 + xiaoshi1 + sha1 + waijia1;
            hunning.setTotal(total);
            hunning.setShuini1(shuini1);
            hunning.setDashi1(dashi1);
            hunning.setZhongshi1(zhongshi1);
            hunning.setXiaoshi1(xiaoshi1);
            hunning.setSha1(sha1);
            hunning.setWaijia1(waijia1);

            double shuini2 = shuini1 / total * 100;
            double dashi2 = dashi1 / total * 100;
            double zhongshi2 = zhongshi1 / total * 100;
            double xiaoshi2 = xiaoshi1 / total * 100;
            double sha2 = sha1 / total * 100;
            double waijia2 = waijia1 / total * 100;

            hunning.setShuini2(shuini2);
            hunning.setDashi2(dashi2);
            hunning.setZhongshi2(zhongshi2);
            hunning.setXiaoshi2(xiaoshi2);
            hunning.setSha2(sha2);
            hunning.setWaijia2(waijia2);


            System.out.println(waijia2);
            System.out.println(waijia1);
            System.out.println(shuini2); //2代表占比
            System.out.println(shuini1); //1代表材料数量


            boolean save = hunningService.save(hunning);
            if (save) {
                return R.ok().data("hunning",hunning);
            } else {
                return R.error();
            }


        }


        if (region==30) {
            double shuini1 = size * (1 + shuini * 0.01) * 333/1000;
            double dashi1 = size * (1 + dashi * 0.01) * 445/1000;
            double zhongshi1 = size * (1 + zhongshi * 0.01) * 444/1000;
            double xiaoshi1 = size * (1 + xiaoshi * 0.01) * 555/1000;
            double sha1 = size * (1 + sha * 0.01) * 452/1000;
            double waijia1 = size * (1 + waijia * 0.01) * 5.32/1000;
            double total = shuini1 + dashi1 + zhongshi1 + xiaoshi1 + sha1 + waijia1;
            hunning.setTotal(total);
            hunning.setShuini1(shuini1);
            hunning.setDashi1(dashi1);
            hunning.setZhongshi1(zhongshi1);
            hunning.setXiaoshi1(xiaoshi1);
            hunning.setSha1(sha1);
            hunning.setWaijia1(waijia1);

            double shuini2 = shuini1 / total * 100;
            double dashi2 = dashi1 / total * 100;
            double zhongshi2 = zhongshi1 / total * 100;
            double xiaoshi2 = xiaoshi1 / total * 100;
            double sha2 = sha1 / total * 100;
            double waijia2 = waijia1 / total * 100;

            hunning.setShuini2(shuini2);
            hunning.setDashi2(dashi2);
            hunning.setZhongshi2(zhongshi2);
            hunning.setXiaoshi2(xiaoshi2);
            hunning.setSha2(sha2);
            hunning.setWaijia2(waijia2);


            System.out.println(waijia2);
            System.out.println(waijia1);
            System.out.println(shuini2); //2代表占比
            System.out.println(shuini1); //1代表材料数量


            boolean save = hunningService.save(hunning);
            if (save) {
                return R.ok().data("hunning",hunning);
            } else {
                return R.error();
            }


        }


        if (region==35) {
            double shuini1 = size * (1 + shuini * 0.01) * 965/1000;
            double dashi1 = size * (1 + dashi * 0.01) * 444/1000;
            double zhongshi1 = size * (1 + zhongshi * 0.01) * 555/1000;
            double xiaoshi1 = size * (1 + xiaoshi * 0.01) * 1222/1000;
            double sha1 = size * (1 + sha * 0.01) * 115/1000;
            double waijia1 = size * (1 + waijia * 0.01) * 5.32/1000;
            double total = shuini1 + dashi1 + zhongshi1 + xiaoshi1 + sha1 + waijia1;
            hunning.setTotal(total);
            hunning.setShuini1(shuini1);
            hunning.setDashi1(dashi1);
            hunning.setZhongshi1(zhongshi1);
            hunning.setXiaoshi1(xiaoshi1);
            hunning.setSha1(sha1);
            hunning.setWaijia1(waijia1);

            double shuini2 = shuini1 / total * 100;
            double dashi2 = dashi1 / total * 100;
            double zhongshi2 = zhongshi1 / total * 100;
            double xiaoshi2 = xiaoshi1 / total * 100;
            double sha2 = sha1 / total * 100;
            double waijia2 = waijia1 / total * 100;

            hunning.setShuini2(shuini2);
            hunning.setDashi2(dashi2);
            hunning.setZhongshi2(zhongshi2);
            hunning.setXiaoshi2(xiaoshi2);
            hunning.setSha2(sha2);
            hunning.setWaijia2(waijia2);


            System.out.println(waijia2);
            System.out.println(waijia1);
            System.out.println(shuini2); //2代表占比
            System.out.println(shuini1); //1代表材料数量


            boolean save = hunningService.save(hunning);
            if (save) {
                return R.ok().data("hunning",hunning);
            } else {
                return R.error();
            }


        }


        if (region==40) {
            double shuini1 = size * (1 + shuini * 0.01) * 1500/1000;
            double dashi1 = size * (1 + dashi * 0.01) * 785/1000;
            double zhongshi1 = size * (1 + zhongshi * 0.01) * 888/1000;
            double xiaoshi1 = size * (1 + xiaoshi * 0.01) * 1562/1000;
            double sha1 = size * (1 + sha * 0.01) * 338/1000;
            double waijia1 = size * (1 + waijia * 0.01) * 5.32/1000;
            double total = shuini1 + dashi1 + zhongshi1 + xiaoshi1 + sha1 + waijia1;
            hunning.setTotal(total);
            hunning.setShuini1(shuini1);
            hunning.setDashi1(dashi1);
            hunning.setZhongshi1(zhongshi1);
            hunning.setXiaoshi1(xiaoshi1);
            hunning.setSha1(sha1);
            hunning.setWaijia1(waijia1);

            double shuini2 = shuini1 / total * 100;
            double dashi2 = dashi1 / total * 100;
            double zhongshi2 = zhongshi1 / total * 100;
            double xiaoshi2 = xiaoshi1 / total * 100;
            double sha2 = sha1 / total * 100;
            double waijia2 = waijia1 / total * 100;

            hunning.setShuini2(shuini2);
            hunning.setDashi2(dashi2);
            hunning.setZhongshi2(zhongshi2);
            hunning.setXiaoshi2(xiaoshi2);
            hunning.setSha2(sha2);
            hunning.setWaijia2(waijia2);


            System.out.println(waijia2);
            System.out.println(waijia1);
            System.out.println(shuini2); //2代表占比
            System.out.println(shuini1); //1代表材料数量


            boolean save = hunningService.save(hunning);
            if (save) {
                return R.ok().data("hunning",hunning);
            } else {
                return R.error();
            }


        }



        return R.ok();

    }


    //分页查询
    //3,分页查询方法
    //current 当前页
    //limit   每页记录数
//    @Cacheable(value = "row", key = "'index'")
    @ApiOperation(value = "分页查询混凝土方法")
    @PostMapping("hunningpage/{current}/{limit}")
    public R pageListHunning(@PathVariable long current,
                             @PathVariable long limit){
        //创建page对象
        Page<Hunning> page = new Page<>(current,limit);
        //调用方法实现分页
        //调用方法时候底层封装，把分页所有数据封装到pageTeacher对象里面
        hunningService.page(page,null);
        long total = page.getTotal();//总记录数
        List<Hunning> records = page.getRecords();//数据list集合
        System.out.println(records);
        return R.ok().data("total",total).data("row",records);

    }

    //根据id查看混凝土记录
    @PostMapping("showId/{id}")
    public R showId(@PathVariable String id){
        Hunning hunning = hunningService.getById(id);
        if(hunning!=null){
            System.out.println(hunning);
            return R.ok().data("hunning",hunning);
        }else {
            return R.error();

        }
    }


    //删除混凝土记录
    @DeleteMapping("{Id}")
    public R delete(@PathVariable String Id) {
        hunningService.removeById(Id);
        return R.ok();
    }

}

